/***************************************************************************
 *   Copyright (C) 2007 by Grzegorz Latarowski, Maciej Gajewski            *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
 
 
#ifndef GNETCLIENT_H
#define GNETCLIENT_H

// Qt
#include <QtGlobal>
#include <QHostAddress>
#include <QTcpSocket>

// local
#include "gnettypes.h"
#include "gnetpackets.h"



// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
class GNetClient : public QObject
{
	Q_OBJECT
			
public:
	// ====================================================
	// constructor/destructor
	
	GNetClient( QObject * pParent = 0 );
	virtual ~GNetClient() {}
	
	
	// ====================================================
	// network conection operations

	/// Conencts to remote host
	virtual void connectToHost( const QString & hostName, quint16 port );

	/// Disconects from remote host
	virtual void disconnectFromHost();

	/// checks if conection is established
	bool isConnected();

	int timeout() { return _timeout; }
	void setTimeout( const int timeout ) { _timeout = timeout; }
	
	
	// ====================================================
	// gnet specific 
	
	/// Processes authorization
	void sendAuthorization( const QString & login, const QString & password );
		
	/// Returns session-unique request id
	quint32 getRequestId();
	
	/// Sends packet
	void sendPacket( const GPacket & packet );
	
	/// Waits for reply packet
	virtual GNetPacketReply * waitForReply( quint32 requestId );

	/// Waits for packet
	virtual GNetPacket * waitForPacket();

	QHostAddress localAddress() { return _socket.localAddress(); }
	
protected slots: 
	/// \brief If socket disconnectes emits signal.
	void socketStateChanged( QAbstractSocket::SocketState socketState );
	
	
signals:
	/// Emmitted when host closes connection.
	void disconnected();
	
	void authorizationFailed( const QString & message );
	
	
private:
	int     _timeout;     ///< Allowable timeout [ms]
	QTcpSocket _socket;   ///< comm socket
	
	quint32 _requestId;   ///< USed to calculate next request id

	
}; // class !GNetClient
// ---------------------------------------------------------------------------


#endif // !GNETCLIENT_H

// eof
